import axios from "axios";
import Vue from 'vue'
import {
  getToken
} from '@/utils/auth'
let request = axios.create({
  // baseURL: "http://192.168.2.103:8098" || process.env.VUE_APP_BASEURL, //优先取前面的值
  // baseURL: "http://192.168.2.103:9002" || process.env.VUE_APP_BASEURL, //优先取前面的值
  baseURL: "/"||  "http://192.168.2.200:9000" ,                          //process.env.VUE_APP_BASEURL,  
  timeout: 1000,
  withCredentials: true,
  headers: {
    "Content-Type": "application/json;charset=UTF-8"
  },
  // axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';        //配置请求头
});

request.interceptors.request.use(
  config => {
    if (config.url != "/login") {
      if (getToken('X-TOKEN') == undefined) {
        config.headers['X-TOKEN'] = "";
      } else {
        config.headers['X-TOKEN'] = getToken('X-TOKEN');
      }
    }
    return config;
  },
  err => {
    return Promise.error(err);
  }
);
request.interceptors.response.use(
  res => {
    if (res.status === 200) {
      if (res.data && res.data.code === 401) {
        Vue.$router.push('/');
      }
      return Promise.resolve(res.data);
    } else {
      return Promise.reject(res.data);
    }
  },
  err => {
    if (err.response) {
      if (err.response.status) {
        switch (err.response.status) {
          // 401: 未登录
          // 未登录则跳转登录页面，并携带当前页面的路径
          // 在登录成功后返回当前页面，这一步需要在登录页操作。\
          case -9999:
            Vue.$router.push('/');
            break;
          case 401:
            Vue.$router.push('/');
            break;
          case 404:
            Vue.$message.error("请求不存在");
            break;
          default:
            Vue.$message.error(err.response.data.message || "请求错误");
        }
        return Promise.reject(err.response);
      }
    } else {
      // Vue.$router.push('/');
      return Promise.reject("请求错误");
    }
  }
);
export default request;
